<?php
/**
 * @title 1143. 最长公共子序列 - 动态规划
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param String $text1
     * @param String $text2
     * @return Integer
     */
    function longestCommonSubsequence($text1, $text2) {
        /**
         * @since 2024-01-03 dp[i][j]，表示text1[0,i-1]和text2[0,j-1]的最长公共子序列的长度
         * 推导dp[i+1][j+1]
         * text1[i] == text2[j]，dp[i+1][j+1] = dp[i][j]+1
         * text1[i] != text2[j]，dp[i+1][j+1] = max(dp[i][j+1], dp[i+1][j])
         */

        /**
         * @since 2024-01-03 字符串长度
         */
        $len1 = strlen($text1);
        $len2 = strlen($text2);

        /**
         * @since 2024-01-03 初始化
         */
        $dp = [];
        for($i=0; $i<=$len1; $i++){
            for($j=0; $j<=$len2; $j++){
                /**
                 * @since 2024-01-03 dp[0][]，就是text[0,-1]，空字符串
                 */
                if($i==0 OR $j==0){
                    $dp[$i][$j] = 0;
                } else {
                    if($text1[$i-1] == $text2[$j-1]){
                        $dp[$i][$j] = $dp[$i-1][$j-1]+1;
                    } else {
                        $dp[$i][$j] = max($dp[$i][$j-1], $dp[$i-1][$j]);
                    }
                }
            }
        }

        /**
         * @return Integer
         */
        return $dp[$len1][$len2];
    }
}


/**
 * @url http://127.0.0.1/leetcode/202401/2024.01.03_16.php
 */
$datas = [
    // ["whydynkfslsrmvyhonyjenyrenojofafmnafmfyhyjebwhqpwh", "ctqdkfctanypmxqxktqfwfgnwjqpsbgpydovufgfqbyvqpufujy"],
    ["abcde", "ace" ], // 3
    // ["aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"], //
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->longestCommonSubsequence(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
